######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# figureVII-analysis.R
#  :: runs the leave-one-year-out IV analysis to produce Figure VII in the online appendix 

rm(list=ls())
setwd('~/Dropbox/rainReplication')

non.missings = 4
fes.type = 3

source('prelimMain.R')       

cnts=0     
yars=1956+2*0:26
coefs_clst=coefs=list()
coefs[[1]]=coefs[[2]]=matrix(NA,length(yars),3)
coefs_clst[[1]]=coefs_clst[[2]]=matrix(NA,length(yars),3)

for(yearX in yars){ 

full=which(rain_data$year>1954 & rain_data$year!=yearX & rain_data$lower & rain_data$r_inc!=rain_data$d_inc)	
cnts=cnts+1  

#detach(covs)                                                                                              
# ANALYSIS     
   
main_iv1_fe=ivreg(vote~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq +redistricted + 
	dose + dose_prv + vote_prv,
	~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq +redistricted + 
	dose_prv + vote_prv + rain_day+rain_day_prev,
	subset=full,data=covs) 
main_iv2_fe=ivreg(vote~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq +redistricted + 
	dose + dose_prv + vote_prv,
	~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq +redistricted + 
	dose_prv + vote_prv + rain_weekend+rain_weekend_prev,
	subset=full,data=covs) 
	
main_iv1_fe_sum=summary(main_iv1_fe)
main_iv2_fe_sum=summary(main_iv2_fe)
                              
main_iv1_fe_sumcl=coeftest.cluster(covs[full,],main_iv1_fe,cluster1='as.factor(fe_id_num)')
main_iv2_fe_sumcl=coeftest.cluster(covs[full,],main_iv2_fe,cluster1='as.factor(fe_id_num)')
          
coefs[[1]][cnts,c(1:3)]=main_iv1_fe_sum$coef[which(rownames(main_iv1_fe_sum$coef)=='dose'),c(1,2,4)]
coefs_clst[[1]][cnts,c(1:3)]=main_iv1_fe_sumcl[which(rownames(main_iv1_fe_sumcl)=='dose'),c(1,2,4)]

coefs[[2]][cnts,c(1:3)]		=main_iv2_fe_sum$coef[which(rownames(main_iv2_fe_sum$coef)=='dose'),c(1,2,4)]
coefs_clst[[2]][cnts,c(1:3)]=main_iv2_fe_sumcl	 [which(rownames(main_iv2_fe_sumcl)=='dose'),c(1,2,4)]
    
}   

colnames(coefs[[1]])=colnames(coefs[[2]])=   
 colnames(coefs_clst[[1]])=colnames(coefs_clst[[2]])=
 c('Beta','SE','P')

rownames(coefs[[1]])=rownames(coefs[[2]])=   
 rownames(coefs_clst[[1]])=rownames(coefs_clst[[2]])=
 yars

save(coefs,coefs_clst,
file=paste('leaveout/figureVII-',non.missings,'_',fes.type,'.Rdata',sep='')) 

rm(coefs,coefs_clst)  
  
#	}
#}
